﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using ECOSYSTEM_Dominio;

namespace ECOSYSTEM_DAL
{
    public class AreaDAL
    {
        SqlConnection cnn;
        public Boolean RegistrarArea(Area elArea) {
            try
            {
                cnn = (SqlConnection)ConexionSQL.getInstancia().getConexion();
                cnn.Open();
                SqlCommand cmd = new SqlCommand("RegistrarArea",cnn);
                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.Add("@AreaID", SqlDbType.Int);
                cmd.Parameters["@AreaID"].Direction = ParameterDirection.InputOutput;
                cmd.Parameters.Add("@NomArea", SqlDbType.NVarChar,50);
                cmd.Parameters.Add("@DescArea", SqlDbType.Text);

                cmd.Parameters["@AreaID"].Value = elArea.IDArea;
                cmd.Parameters["@NomArea"].Value = elArea.Nombre;
                cmd.Parameters["@DescArea"].Value = elArea.Descripcion;

                cmd.ExecuteNonQuery();
                elArea.IDArea = (Int32)(cmd.Parameters["@AreaID"].Value);
            }
            catch (SqlException ex) { throw new Exception(ex.Message); }
            catch (Exception e) { throw new Exception(e.Message); }
            finally {
                if (cnn.State == ConnectionState.Open) {
                    cnn.Close();
                }
            }
            return true;
        }

        public DataTable ObtenerArea() {
            DataTable lista = null;
            try
            {
                cnn = (SqlConnection)ConexionSQL.getInstancia().getConexion();
                cnn.Open();
                SqlDataAdapter da = new SqlDataAdapter("ObtenerArea",cnn);
                da.SelectCommand.CommandType = CommandType.StoredProcedure;
                lista = new DataTable();
                da.Fill(lista);
            }
            catch (SqlException ex) { throw new Exception(ex.Message); }
            catch (Exception e) { throw new Exception(e.Message); }
            finally {
                if (cnn.State == ConnectionState.Open) {
                    cnn.Close();
                }
            }
            return lista;
        }


        public Boolean ActualizarArea(Area elArea, Int32 CodigoAre)
        {
            try
            {
                cnn = (SqlConnection)ConexionSQL.getInstancia().getConexion();
                cnn.Open();
                SqlCommand cmd = new SqlCommand("ActulizarArea", cnn);
                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.Add("@NomArea", SqlDbType.VarChar, 30);
                cmd.Parameters.Add("@DescArea", SqlDbType.Text);
                cmd.Parameters["@NomArea"].Value = elArea.Nombre;
                cmd.Parameters["@DescArea"].Value = elArea.Descripcion;
                cmd.Parameters.AddWithValue("@CodigoArea", CodigoAre);
                cmd.ExecuteNonQuery();
            }
            catch (SqlException e) { throw new Exception(e.Message); }
            catch (Exception ex) { throw new Exception(ex.Message); }
            finally
            {
                if (cnn.State == ConnectionState.Open)
                {
                    cnn.Close();
                }
            }
            return true;
        }
    }
}
